package com.example.kafkademo.controller;

import com.example.kafkademo.common.MessageEntity;
import com.example.kafkademo.producer.SimpleProducer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("kafka")
public class KafkaTestController {

    @Autowired
    private SimpleProducer simpleProducer;

    @GetMapping("hello")
    public ResponseEntity testHello() {
        System.out.println("Hello");
        return new ResponseEntity("hello", HttpStatus.OK);
    }

    @PostMapping("send")
    public ResponseEntity testSend() {
        try {
            MessageEntity message = new MessageEntity("titleceshi", "neirongceshi");
            simpleProducer.send("test-kafka", "kafkaDemo", message);

            return new ResponseEntity(HttpStatus.OK);
        } catch (Exception e) {
            e.printStackTrace();
            return new ResponseEntity(null, HttpStatus.INTERNAL_SERVER_ERROR);
        }
    }

}
